﻿using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using Abp.Runtime.Caching;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
using NetCore.Entities.LocationAreas;
using NetCore.Extensions;
using NetCore.LocationAreas.Dtos;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NetCore.LocationAreas
{
    /// <summary>
    /// 区域服务
    /// </summary>
    public class LocationAreaAppService : ILocationAreaAppService
    {
        private readonly IRepository<LocationArea> _locationAreaRepository;
        private readonly ICacheManager _cacheManager;
        //private readonly IUserManager _userManager;
        public LocationAreaAppService(
            //IUserManager userManager,
            ICacheManager cacheManager,
            IRepository<LocationArea> locationAreaRepository)
        {
            _locationAreaRepository = locationAreaRepository;
            _cacheManager = cacheManager;
            //_userManager = userManager;
        }

        /// <summary>
        /// 获取省市区区域列表公共接口 最多带一层子集
        /// </summary>
        /// <returns></returns>
        public async Task<IList<LocationAreaDto>> GetLocationAreas(LocationAreaInput input)
        {
            #region 使用redis缓存 数据量太大,不适合使用redis

            ////有了level之后就不需要有ParentId了
            //input.ParentId = input.Level.HasValue ? default(long?) : input.ParentId;

            var cackKey = $"_{input.Level}{input.ParentId}{input.ParentCode}_Area";
            var areas = await _cacheManager.GetCache(NetCoreConsts.LocationArea).GetAsync(cackKey, async () =>
            {
                return await _locationAreaRepository.GetAll()
                    //.GetAllIncluding(m => m.Children, n => n.Parent)
                    //.Where(n => n.ParentId == input.ParentId)
                    .WhereIf(input.Level.HasValue, m => m.Level == input.Level)
                    .WhereIf(input.ParentId.HasValue, m => m.ParentId == input.ParentId)
                    .WhereIf(input.ParentCode.HasValue(), m => m.Parent.Code.Equals(input.ParentCode))
                    //.WhereIf(input.ParentName.HasValue(), m => m.Parent.Name.Contains(input.ParentName))
                    //.WhereIf(input.Code.HasValue(), m => m.Code.Contains(input.Code))
                    //.WhereIf(input.Name.HasValue(), m => m.Name.Contains(input.Name))
                    //.WhereIf(input.Pinyin.HasValue(), m => m.Pinyin.Contains(input.Pinyin))
                    //.OrderBy(o => o.PinyinPrefix)
                    .ProjectTo<LocationAreaDto>()
                    .ToListAsync();
            });

            #endregion

            #region 从数据库中直接取

            //var areas = await _locationAreaRepository
            //    //.GetAllIncluding(m => m.Children, n => n.Parent)
            //    .GetAll()
            //    .Where(n => n.ParentId == input.ParentId)
            //    //.WhereIf(input.Level.HasValue, m => m.Level == input.Level)
            //    //.WhereIf(input.ParentId.HasValue, m => m.ParentId == input.ParentId)
            //    //.WhereIf(input.ParentName.HasValue(), m => m.Parent.Name.Contains(input.ParentName))
            //    //.WhereIf(input.Code.HasValue(), m => m.Code.Contains(input.Code))
            //    //.WhereIf(input.Name.HasValue(), m => m.Name.Contains(input.Name))
            //    //.WhereIf(input.Pinyin.HasValue(), m => m.Pinyin.Contains(input.Pinyin))
            //    //.OrderBy(o => o.PinyinPrefix)
            //    .ProjectTo<LocationAreaDto>()
            //    .ToListAsync();

            #endregion

            return areas;
        }
    }
}
